package se.slp.test1;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DataHelper {

	private static final String DATABASE_NAME = "slpDatabase.db";
	private static final int DATABASE_VERSION = 1;
	private static final String TABLE_NAME = "account";
	private static String name_forSearch ;

	private Context context;
	private SQLiteDatabase db;
	
	private int id_row;

	private SQLiteStatement insertStmt;
	private static final String INSERT = "insert into " + TABLE_NAME
			+ "(name,weight,height,sex,bmi,filename,date) values(?,?,?,?,?,?,?)";

	public DataHelper(Context context) {
		this.context = context;
		OpenHelper openHelper = new OpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
	}

	public long insert(String name,String weight,String height,String sex,String bmi,String filename,String date_now) {

		
		this.insertStmt.bindString(1, name);
		this.insertStmt.bindString(2, weight);
		this.insertStmt.bindString(3, height);
		this.insertStmt.bindString(4, sex);
		this.insertStmt.bindString(5, bmi);
		this.insertStmt.bindString(6, filename);
		this.insertStmt.bindString(7, date_now);
		return this.insertStmt.executeInsert();
			
	}
	
	public void getID(){
		
	}

	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}
	
	public void deleteRow(String receip_name){
		name_forSearch = "name = "+"'"+receip_name+"'";
		this.db.delete(TABLE_NAME, name_forSearch , null);
		
	}
	
	
	public List<String> selectAll() {
		List<String>list = new ArrayList<String>();
		Cursor cursor = this.db.query(TABLE_NAME,null,
				null, null, null, null, "id desc");

		if (cursor.moveToFirst()) {

			do {
				
				 list.add(cursor.getString(1));
				//list.add(cursor.getString(7));

			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return list;
	}
	
	
	public List<String> selectRow(String receip_name) {
	   name_forSearch = "name = "+"'"+receip_name+"'";
		List<String>list = new ArrayList<String>();
		
		Cursor cursor = this.db.query(TABLE_NAME,null,
				name_forSearch, null, null, null, "id desc");
		
		if (cursor.moveToFirst()) {

			do {
				
				list.add(cursor.getString(1));
				list.add(cursor.getString(2));
				list.add(cursor.getString(3));
				list.add(cursor.getString(4));
				

			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return list;
	}
	
	private static class OpenHelper extends SQLiteOpenHelper {

		OpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE " + TABLE_NAME + 
          "(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT Unique,weight TEXT,height TEXT,sex TEXT,bmi TEXT,riskRate TEXT,date TEXT);");
      }

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w("slpDatabase",
					"Upgrading database, this will drop tables and recreate.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	}
}
